#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2022/5/12 18:00
@File: list.py
"""

from qturboframe.lib.utils.check import CheckBasic
from qturboframe.lib.webdriver import SeleniumError

from BladeFerm.hui_app.hui_lib.list import List as BladeList

from BladeFerm.Lib.Common import logger
from BladeFerm.hui12_common.function import get_params


class List(BladeList, CheckBasic):
    def __init__(self, element_para, father=None, timeout=5):
        super().__init__(element_para, father, timeout)
        self.list_type = self._element
        self.res_elements = None

    def foud_target(self, idname=None):
        by, value, assist = get_params(idname)
        self.res_elements = self.list_type.find_elements_by_attribute(by, value, assist)

    def click_target(self, text):
        '''
        点击目标元素，选中操作
        :param text:
        :return:
        '''
        for unit in self.res_elements:
            logger.info(f'found text : {unit.text}')
            if unit.text == text:
                unit.click()
                return True
        else:
            logger.warning(f'no found ele : {text}')
            return False

    def select_target(self, text):
        '''
        勾选多个目标值
        :param text: 'a,v,c'  // 英文的逗号分割
        :return: 选中的文本值列表
        '''
        res = []
        text_list = text.split(',')
        for unit in self.res_elements:
            logger.info(f'found text : {unit.text}')
            if unit.text in text_list:
                unit.click()
                res.append(unit.text)
                logger.info(f'select text : {unit.text}')
        return res
